var date = [
    {  receiptNumber: '20220704003', fileNumber: '012022000001', location: '001-001-01-01', reason: '说明原因',type:'移库',staff:'李志勇',time:'2022-07-12' },
    {  receiptNumber: '20220704003', fileNumber: '012022000002', location: '001-001-01-02', reason: '说明原因',type:'移库',staff:'张三',time:'2022-07-12' },
    {  receiptNumber: '20220704003', fileNumber: '012022000003', location: '001-001-01-03', reason: '说明原因',type:'移库',staff:'李四',time:'2022-07-13' },
    { receiptNumber: '20220704004', fileNumber: '012022000004', location: '001-001-01-03', reason: '说明原因',type:'移库',staff:'赵武',time:'2022-07-14' },
    { receiptNumber: '20220704004', fileNumber: '012022000005', location: '001-001-01-03', reason: '说明原因',type:'移库',staff:'钱刘',time:'2022-07-14' },
    {  receiptNumber: '20220704005', fileNumber: '012022000006', location: '001-001-01-04', reason: '说明原因',type:'移库',staff:'刘备',time:'2022-07-15' },
    {  receiptNumber: '20220704005', fileNumber: '012022000007', location: '001-001-01-04', reason: '说明原因',type:'移库',staff:'关于',time:'2022-07-15' },
    { receiptNumber: '20220704006', fileNumber: '012022000008', location: '001-001-01-05', reason: '说明原因',type:'移库',staff:'张飞',time:'2022-07-15' },
    {  receiptNumber: '20220704006', fileNumber: '012022000009', location: '001-001-01-06', reason: '说明原因',type:'移库',staff:'李志勇',time:'2022-07-16' },
    {  receiptNumber: '20220704007', fileNumber: '0120220000010', location: '001-001-01-06', reason: '说明原因',type:'移库',staff:'吕小布',time:'2022-07-17' },
    { receiptNumber: '20220704007', fileNumber: '0120220000011', location: '001-001-01-07', reason: '说明原因',type:'移库',staff:'张昭',time:'2022-07-17' },
    { receiptNumber: '20220704008', fileNumber: '0120220000012', location: '001-001-01-08', reason: '说明原因',type:'移库',staff:'赵云',time:'2022-07-18' },
    { receiptNumber: '20220704009', fileNumber: '0120220000012', location: '001-001-01-08', reason: '说明原因',type:'移库',staff:'孙权',time:'2022-07-18' },
    {  receiptNumber: '202207040010', fileNumber: '0120220000013', location: '001-001-01-09', reason: '说明原因',type:'移库',staff:'赵肖',time:'2022-07-19' },
    {  receiptNumber: '202207040010', fileNumber: '0120220000014', location: '001-001-01-09', reason: '说明原因',type:'移库',staff:'曹操',time:'2022-07-19' },
    {  receiptNumber: '202207040011', fileNumber: '0120220000015', location: '001-001-01-10', reason: '说明原因',type:'移库',staff:'赵义',time:'2022-07-20' },
    {  receiptNumber: '202207040012', fileNumber: '0120220000016', location: '001-001-01-11', reason: '说明原因',type:'移库',staff:'孙伯符',time:'2022-07-20' },
    {  receiptNumber: '202207040013', fileNumber: '0120220000016', location: '001-001-01-12', reason: '说明原因',type:'移库',staff:'周瑜',time:'2022-07-21' },

];
//当前的页码
var page = 1;
//一页的条数
var count = 8;
var oTbody = document.querySelector('tbody');
var oPage = document.querySelector('.pagination');
var oNextPage = document.querySelector('.next');
var oPrevPage = document.querySelector('.prev');
var oSearchInp=document.querySelector('.searchInp');
var oSearchInp2=document.querySelector('.searchInp2');
var oSearchInp3=document.querySelector('.searchInp3');
var oSearchInp4=document.querySelector('.searchInp4');
var dateClone = cloneArr(date);
var oSearchBtn=document.querySelector('.searchBtn');
var oAddBtn=document.querySelector('.addBtn');
var oResetBtn=document.querySelector('.resetBtn');
var oSaveReset=document.querySelector('.saveResetBtn');
//渲染数据和页码
function render(){
    var str1=oSearchInp.value;
    var str2=oSearchInp2.value;
    var str3=oSearchInp3.value;
    var str4=oSearchInp4.value;
    for(var i=0;i<date.length;i++){
        date[i].id=i+1
    }
    
    var date1=[];
    var date2=[];
    var date3=[];
    var searchDate=[];

    if(str1.trim()==''){
        date1=date;
    }else{
        date1=date.filter(function (v){
           
            return v.receiptNumber.includes(str1)
        })
    }

    if(str2.trim()==''){
        date2=date1;
    }else{
        date2=date1.filter(function (v){
          
            return v.fileNumber.includes(str2);
        })
    }
    if(str3.trim()==''){
        date3=date2;
    }else{
        date3=date2.filter(function (v){
            
            return v.staff.includes(str3)
        })
    }
    if(str4.trim()==''){
        searchDate=date3;
    }else{
        searchDate=date3.filter(function (v){
            return v.time.includes(str4)
        })
    }
   
    
   
    oTbody.innerHTML='';
    var renderArr = searchDate.slice((page - 1) * count, page * count);
    for (var i = 0; i < renderArr.length; i++) {
    var oTr = document.createElement('tr');
    oTr.innerHTML = `
                <td>${renderArr[i].id}</td>
                <td>${renderArr[i].receiptNumber}</td>
                <td>${renderArr[i].fileNumber}</td>
                <td>${renderArr[i].location}</td>
                <td>${renderArr[i].reason}</td>
                <td>${renderArr[i].type}</td>
                <td>${renderArr[i].staff}</td>
                <td>${renderArr[i].time}</td>
                
    `;
    oTbody.appendChild(oTr);
}
//渲染页码
var aPageBtn=document.querySelectorAll('.pageBtn');
for(var i=0;i<aPageBtn.length;i++){
    oPage.removeChild(aPageBtn[i].parentNode);
}
for (var i = 1; i <= Math.ceil(searchDate.length / count); i++) {
    var oLi = document.createElement('li');
    oLi.innerHTML = `<a class="pageBtn" >${i}</a>`;
    //oLi.classList.add('pageBtn');
    if(i==page){
        oLi.classList.add('active');
    }
    oPage.insertBefore(oLi,oPage.lastElementChild);

}
}
render();
//给分页加事件
oPage.addEventListener('click',changePage);
function changePage(){
    if(event.target.className=='pageBtn'){
        // var aPageBtn=document.querySelectorAll('.pageBtn');
        // for(var i=0;i<aPageBtn.length;i++){
        //     aPageBtn[i].parentNode.classList.remove('active');
        // }
        // event.target.parentNode.classList.add('active');

        page=event.target.innerHTML;
        render();
    }
}
//上一页下一页事件
oPrevPage.addEventListener('click',prevFn);
function prevFn(){
    if(page>1){
        page--;
        render();
    }
}
oNextPage.addEventListener('click',nextFn);
function nextFn(){
    if(page<Math.ceil(date.length / count)){
        page++;
        render();
    }
}
//搜索事件
oSearchBtn.addEventListener('click',searchFn);
function searchFn(){
   page=1;
   render();
}
//入库保存事件
oAddBtn.addEventListener('click',addFn);
function addFn(){
    var addId=document.querySelector('.addId').value;
    var addFile=document.querySelector('.addFile').value;
    var addLocation=document.querySelector('.addLocation').value;
    var addState=document.querySelector('.addState').value;

    date.push({
        receiptNumber:addId,
        fileNumber:addFile,
        location:addLocation,
        state:addState,
    })
    render();
    $('#addModal').modal('hide');
}
//重置事件
oResetBtn.addEventListener('click',resetFn);
function cloneArr(obj){
   return JSON.parse(JSON.stringify(obj));
}
function resetFn(){
    $('#resetModal').modal('show');
   
}
oSaveReset.addEventListener('click',saveReset);
function saveReset(){
     var formInp=document.querySelectorAll('.input-area input');
    formInp[0].value = "";
    formInp[1].value = "";
    dateClone=cloneArr(date);
    $('#resetModal').modal('hide');
    render(dateClone);
}